<? 
/* place any include files below */ 
include_once("../include/session.php");
include_once("../include/dbConnect.php");
include_once("../include/class/FileUpload.class.php");
/* checking if this file can't be accessed directly */ 
if(ereg("eventProcess",$_SERVER['PHP_SELF'])) exit();
/* receive post variable */ 
$action=$_POST["action"];
$id=$_POST["id"];
/* if action is add new */ 
if($action=="new"){
/* check if record is duplicated */ 
	 $id=trim($_POST["event_id"]);
	 $query_check=mysql_query("SELECT event_id FROM event WHERE event_id='$id'");
	 if(mysql_num_rows($query_check)){ 
	 	 echo "<script>alert('save failed because record is duplicated!')</script>";
	 	 exit();
	 } 
	 $event_tgl=strip_tags(trim(nl2br($_POST['event_tgl'])));
	 $event_ket=strip_tags(trim(nl2br($_POST['event_ket'])));
	 $event_color=strip_tags(trim(nl2br($_POST['event_color'])));
	 $sql="INSERT INTO event(event_color,event_tgl,
 	 	   event_ket)
	 	   VALUES('".$event_color."','".$event_tgl."', 
 	 	  '".$event_ket."' )";
	 	 $result=mysql_query($sql);
/* caught exception of result */ 
	 if($result){
	 	 echo "<script>alert('new record added')</script>";
	 }else{
	 	 echo "<script>alert('record save failed')</script>";
	 } 
}
/* if action is edit */ 
 else if($action=="edit"){
	 $id=$_POST['event_id'];
	 $event_tgl=strip_tags(trim(nl2br($_POST['event_tgl'])));
	 $event_ket=strip_tags(trim(nl2br($_POST['event_ket'])));
	 $sql="UPDATE event SET 
 	 	  event_tgl='".$event_tgl."',
 	 	  event_color='".$event_color."',
 	 	  event_ket='".$event_ket."'
 	 	  WHERE event_id='$id'"; 
	 $result=mysql_query($sql); 
/* caught exception of result */ 
	 if($result){ 
	 	 echo "<script>alert('record updated')</script>"; 
	 }else{
	 	 echo "<script>alert('record save failed')</script>";
	 }
}
/* if action is delete */ 
 else if($action=="delete")
{
	 $sql="delete from event  WHERE event_id='$id'";
	 $result=mysql_query($sql);
	 if($result){
	 	 echo "<script>alert('record deleted')</script>";
	 }else{
	 	 echo "<script>alert('record deleted failed')</script>";
	 }
}
?> 
